home *** CD-ROM | disk | FTP | other *** search
- Path: pubxfer.news.psi.net!usenet
- From: lif@lutz.com
- Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
- Subject: Re: Tough FACTORIAL math problem...
- Date: Thu, 15 Feb 1996 12:44:36 GMT
- Message-ID: <4fv9m9$h7f@client1.news.psi.net>
- References: <4fr8be$ass@news.iconn.net>
- NNTP-Posting-Host: 206.0.7.4
- X-Newsreader: Forte Free Agent 1.0.82
-
- thecrow@iconn.net (The Crow) wrote:
-
- >Here is what I am trying to do, I want to find the last NON-ZERO digit of a
- >given factorial. For instance,
-
- >5! = 120
-
- >so the last non-zero digit is 2. I want to be able to do this up to 1000.
- >Problem is, no matter how huge of a data-type you use, there isn't any way for
- >the computer to handle 1000!, it is however possible to find the last non-zero
- >digit somehow. One thing I have tried is as I went through mulitplying the
- >series of numbers in the factorial (5 * 4 * 3 * 2) I would remove all the
- >trailing ZEROS, I got this to work up to 789, but it wont work with 1000 and i
- >am not really sure why. If anyone has a clue how I can do this let me know.
-
- >--
- >The Crow - thecrow@iconn.net
- >"It can't rain all the time"
- >-Kryptology
-
- program TryThis(input,output);
-
- function RMNZD(
- n : integer
- ) : integer;
- { determine the right most non-zero digit } begin
- while n mod 10 = 0 do
- n:=n div 10;
- RMNZD:=N mod 10;
- end { RMNZD };
-
- var
- digit : byte;
- i : integer;
- n : integer;
- { } begin
- readln(input,n);
- if n > 1 then { only treat n > 1 } begin
- digit:=1;
- for i:=2 to n do
- digit:=RMNZD(digit)*RMNZD(i);
- writeln(output,RMNZD(digit));
- end;
- end { TryThis }.
-
-